package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.gxhl.api.core.model.GDPResponse;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.ZhongChenYuanServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "zhongchenyuan")
public class ZhongChenYuanAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(ZhongChenYuanAdaptor.class);
	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String proKey = orderBean.getProductCode();//充值的流量套餐编码或话费金额（元）
		String mobile = orderBean.getPhoneNo();
		String orderNo = orderBean.getIds();//订购流水号
		String bcallbackUrl = BetweenProperties.CALLBACK_URL+"Zhongcy";
		String productId = orderBean.getVasProdId();//
		String result = ZhongChenYuanServiceImpl.service.order(proKey, mobile, orderNo,bcallbackUrl,productId);
		log.info("between- 中琛源订单ZhongChenYuanService.order() 订购响应response<>:" + result);
		
		
		JSONObject json = JSONObject.fromObject(result);
		String code = json.getString("code");

		if ("100".equals(code)) {

			orderBean.setMessage(json.getString("tip"));
			orderBean.setErrorMsg(json.getString("tip"));
			orderBean.setStatus("11");
		} else {
			orderBean.setMessage(json.getString("tip"));
			orderBean.setErrorMsg(json.getString("tip"));
			orderBean.setStatus("8");
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	
	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		GDPResponse resultGDP = new GDPResponse();

		String cstmOrderNo = orderBean.getIds();

		String result = ZhongChenYuanServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 中琛源状态ZhongChenYuanService.orderStatus() 查询响应response<> :" + result);
	
		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("code");

		if ("00".equals(code)) {
			orderBean.setMessage(json.getString("tip"));
			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
		} else if ("100".equals(code)) {
			orderBean.setMessage(json.getString("tip"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("1");
		} else {
			orderBean.setMessage(json.getString("tip"));
			orderBean.setErrorMsg(json.getString("tip"));
			orderBean.setStatus("8");
		}
		log.info("between- 中琛源状态ZhongChenYuanService.orderStatus() 查询处理后result<>:"+ new Gson().toJson(resultGDP));
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
